package com.videri.match.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.videri.match.client.MatchSink.MatchSinkInfo;
import java.util.*;

/**
 * This class will maintain a list of the different sinks
 * currently contained in the MatchSink.
 * @author Michael Bauer
 *
 */
public class MatchSinkList extends Composite {
	 private class MouseLink extends Hyperlink {

		    private int index;

		    public MouseLink(String name, int index) {
		      super(name, name);
		      this.index = index;
		      sinkEvents(Event.MOUSEEVENTS);
		    }

		    public void onBrowserEvent(Event event) {
		      switch (DOM.eventGetType(event)) {
		        case Event.ONMOUSEOVER:
		          mouseOver(index);
		          break;

		        case Event.ONMOUSEOUT:
		          mouseOut(index);
		          break;
		      }

		      super.onBrowserEvent(event);
		    }
		  }

		  private HorizontalPanel list = new HorizontalPanel();
		  private ArrayList sinks = new ArrayList();

		  private int selectedSink = -1;

		  public MatchSinkList() {
		    initWidget(list);
		    HorizontalPanel hp = new HorizontalPanel();
		    hp.setSize("0px", "0px");
		    list.add(hp);
		    
		    setStyleName("match-List");
		  }

		  public void addSink(final MatchSinkInfo info) {
		    String name = info.getName();
		    int index = list.getWidgetCount() - 1;

		    MouseLink link = new MouseLink(name, index);
		    list.add(link);
		    sinks.add(info);

		    list.setCellVerticalAlignment(link, HorizontalPanel.ALIGN_BOTTOM);
		    styleSink(index, false);
		  }

		  public MatchSinkInfo find(String sinkName) {
		    for (int i = 0; i < sinks.size(); ++i) {
		      MatchSinkInfo info = (MatchSinkInfo) sinks.get(i);
		      if (info.getName().equals(sinkName)) {
		        return info;
		      }
		    }

		    return null;
		  }

		  public void setSinkSelection(String name) {
		    if (selectedSink != -1) {
		      styleSink(selectedSink, false);
		    }

		    for (int i = 0; i < sinks.size(); ++i) {
		      MatchSinkInfo info = (MatchSinkInfo) sinks.get(i);
		      if (info.getName().equals(name)) {
		        selectedSink = i;
		        styleSink(selectedSink, true);
		        return;
		      }
		    }
		  }

		  private void colorSink(int index, boolean on) {
		    String color = "";
		    if (on) {
		      color = ((MatchSinkInfo) sinks.get(index)).getColor();
		    }

		    Widget w = list.getWidget(index + 1);
		    DOM.setStyleAttribute(w.getElement(), "backgroundColor", color);
		  }

		  private void mouseOut(int index) {
		    if (index != selectedSink) {
		      colorSink(index, false);
		    }
		  }

		  private void mouseOver(int index) {
		    if (index != selectedSink) {
		      colorSink(index, true);
		    }
		  }

		  private void styleSink(int index, boolean selected) {
		    String style = (index == 0) ? "match-FirstSinkItem" : "match-SinkItem";
		    if (selected) {
		      style += "-selected";
		    }

		    Widget w = list.getWidget(index + 1);
		    w.setStyleName(style);

		    colorSink(index, selected);
		  }
}
